/*
 * @Descripttion: 
 * @Author: cuidm
 * @Date: 2022-08-08 13:35:23
 * @LastEditors: cuidm
 * @LastEditTime: 2022-08-15 17:28:51
 */
import React, { useState, useEffect } from "react";
import { Modal, Form, Button, Input,  message, Space, Radio } from "antd";
import { ModelFormItem } from "@/components";
import cmdbres from "@/data/cmdbres";
import request from "@/utils/request";
import regexpUtil from "@/utils/regexpUtil";
import hcpUtils from "@/utils/hcpUtils";

const user = hcpUtils.getUser();
const CreateNode = ({info, visible, setVisible, onCreateFinish})  => {
	const [form] = Form.useForm();
	const [loading, setLoading] = useState(false);

	const onFinish = async (values) => {
		setLoading(true);
		let param = {
			instanceName: values.instanceName,
			bizId: info.bizId,
			cmModuleId: info.nextModuleId,
			moduleName: info.nextModuleName,
			moduleCode: info.nextModuleCode,
			parentId: info.instanceId,
			author: user.staffName
		}
		let res = await request({
			...cmdbres.createMainLineInstance,
			data: param
		});
		setLoading(false);
		if (res && res.code == 200) {
			setVisible(false);
			message.success(`新建节点成功！`);
			if (typeof onCreateFinish == "function") onCreateFinish(res);
		}
	};

	const onCancel = () => {
		setVisible(false);
	}

	return (
		<Modal
			title="新建节点"
			className="hcp-modal-sm"
			visible={visible}
			footer={null}
			onCancel={onCancel}
			destroyOnClose
			maskClosable={false}
			afterClose={()=>{form.resetFields()}}
		>
			<Form
				form={form}
				labelCol={{ span: 5 }}
				wrapperCol={{ span: 18 }}
				name="basic"
				onFinish={onFinish}
			>
				<Form.Item label="已选择">
					<span>{info?.instanceName??'-'}</span>
				</Form.Item>
				{/* <ModelFormItem cmModuleId={info?.nextModuleId}/> */}
				<Form.Item 
					label="实例名" 
					name="instanceName"
					rules={[
						{
							required: true,
							message: '名称不能为空',
						},
					]}
				>
					<Input placeholder="请输入名称" maxLength={256}/>

				</Form.Item>
				<div className="hcp-modal-body-footer">
					<Form.Item wrapperCol={{ span: 24 }} >
						<Button onClick={onCancel}>取消</Button>
						<Button type="primary" htmlType="submit" loading={loading}>提交</Button>
					</Form.Item>
				</div>
			</Form>
		</Modal>
	)
}

export default CreateNode;
